﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Pixer
{
    class Ellipse : Shape
    {
        protected Point center;
        /// <summary>
        /// 椭圆圆心
        /// </summary>
        public Point Center
        {
            get { return center; }
            set { center = value; }
        }

        protected int height;
        /// <summary>
        /// 椭圆的高
        /// </summary>
        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        protected int width;
        /// <summary>
        /// 椭圆的宽
        /// </summary>
        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        #region Shape 成员

        public override void paint(System.Drawing.Graphics g)
        {
            g.DrawEllipse(Pens.Black, center.X - width, center.Y - height, width * 2, height * 2);
        }


        public override void paint(Bitmap b)
        {
            int xc = center.X;
            int yc = center.Y;
            int a, ab;
            if (width > height)
            {
                a = width;
                ab = height;
            }
            else
            {
                a = height;
                ab = width;
            }
            int aa = a * a;
            int bb = ab * ab;
            int twoaa = 2 * aa;
            int twobb = 2 * bb;
            int x = 0;
            int y = ab;
            int dx = 0;
            int dy = twoaa * y;
            int d = (int)(bb + aa * (-ab + 0.25) + 0.5);
            WholeEllipse(xc, yc, x, y, lineColor, b);
            while (dx < dy)
            {
                x++;
                dx += twobb;
                if (d < 0)
                    d += bb + dx;
                else
                {
                    y--;
                    dy -= twoaa;
                    d += bb + dx - dy;
                }
                WholeEllipse(xc, yc, x, y, lineColor, b);
            }
            d = (int)(bb * (x + 0.5) * (x + 0.5) + aa * (y - 1) * (y - 1) - aa * bb + 0.5);
            while (y > 0)
            {
                y--;
                dy -= twoaa;
                if (d > 0)
                    d += aa - dy;
                else
                {
                    x++;
                    dx += twobb;
                    d += aa - dy + dx;
                }
                WholeEllipse(xc, yc, x, y, lineColor, b);
            }

        }
        #endregion
        private void WholeEllipse(int xc, int yc, int x, int y, Color lineColor, Bitmap b)
        {
            try
            {
                b.SetPixel(xc + x, yc + y, lineColor);
                b.SetPixel(xc - x, yc + y, lineColor);
                b.SetPixel(xc + x, yc - y, lineColor);
                b.SetPixel(xc - x, yc - y, lineColor);
            }
            catch
            { }
        }



    }
}
